import re
import json
from urllib import request, parse
from urllib.parse import quote, unquote
import os
import shutil
import sys

# 多少条一页
rn = 30
# 页码
pageNum = 1
# 关键字
key = '景色'
keyword = quote(key)
# 路径地址
folderUrl = 'https://gitee.com/search/translate'
params = {
    'q': key
}
textmod = parse.urlencode(params)
req = request.Request(url='{0}{1}{2}'.format(folderUrl, '?', textmod))
res = request.urlopen(req).read()
res = json.loads(res)
folder = res['result'] + '-' + key
if os.path.exists(folder):
    dirPath = os.path.join(os.path.dirname(__file__), folder)
    shutil.rmtree(dirPath)

os.mkdir(folder)
print('文件夹为', folder)

# 二进制写入
# f = open('./data.js', 'wb')
# f.write(res)
num = 0


def getImg(data):
    global num
    global folder
    for item in data:
        # 正则匹配标题的正确
        # reg = r'([\u4e00-\u9fa5\w-]+)\.[jpg|png|jpeg|gif]+'
        reg = r'[\\:\*\?\"\<\>\|\/\s]*'
        reg = re.compile(reg)
        if item:
            newTitle = item['fromPageTitleEnc'] + \
                '-' + item['di'] + '.' + item['type']
            title = re.sub(reg, '', newTitle)
            request.urlretrieve(
                item['thumbURL'], folder+'/' + title, _progress)
            num += 1
            print('图片数量', num)


def _progress(block_num, block_size, total_size):
    '''回调函数
       @block_num: 已经下载的数据块
       @block_size: 数据块的大小
       @total_size: 远程文件的大小
    '''
    sys.stdout.write('\r>> 图片大小{0}，下载进度{1:.2f}%'.format(
        total_size, float(block_num * block_size/total_size) * 100.0))
    sys.stdout.flush()


def init():
    global pageNum
    for k in range(0, pageNum):
        # 百度地址
        url = 'https://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&ct=201326592&is=&fp=result&queryWord=' + keyword + \
            '&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=-1&z=&ic=0&word=' + keyword + \
            '&s=&se=&tab=&width=&height=&face=0&istype=2&qc=&nc=1&fr=&pn=' + \
            str(rn*k) + '&rn=' + str(rn)
        res = request.urlopen(url).read()
        jsonData = json.loads(res)['data']
        getImg(jsonData)


init()
print('数据爬取完成'.center(50, '*'))
